package com.atguigu.eduservice.controller.frontController;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.MemeberClient;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.service.impl.EduCommentService;
import com.atguigu.entity.UcenterMember;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.security.krb5.internal.ccache.CCacheOutputStream;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @author cyt
 * @create 2020-06-01 15:49
 */
@Slf4j

@RestController
@RequestMapping("/eduservice/comment")
public class CourseCommentController {
    @Autowired
    EduCommentService eduCommentService;
    @Autowired
    MemeberClient memeberClient;
    // 显示评论

    //根据课程id查询评论列表
    @ApiOperation(value = "评论分页列表")
    @GetMapping("{page}/{limit}")
    public R index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                    String courseId) {
        QueryWrapper<EduComment> commentquery = new QueryWrapper<>();

        System.out.println("comment courseid " + courseId);
        commentquery.eq("course_id", courseId);

        Page<EduComment> eduCommentPage = new Page<>(page, limit);

        // 查询
        eduCommentService.page(eduCommentPage, commentquery);

        HashMap<String, Object> map = new HashMap<>();
        map.put("items", eduCommentPage.getRecords());
        map.put("current", eduCommentPage.getCurrent());
        map.put("pages", eduCommentPage.getPages());
        map.put("size", eduCommentPage.getSize());
        map.put("total", eduCommentPage.getTotal());
        map.put("hasNext", eduCommentPage.hasNext());
        map.put("hasPrevious", eduCommentPage.hasPrevious());
        return R.ok().data(map);
    }


    // 添加
    @ApiOperation(value = "添加评论")
    @PostMapping("auth/save")
    public R saveComment(@RequestBody EduComment comment, HttpServletRequest request) {

        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberIdByJwtToken)) {
            return R.error().code(28004).message("请登录");
        }

        comment.setMemberId(memberIdByJwtToken);
        UcenterMember userInfo = memeberClient.getUserInfo(memberIdByJwtToken);

        comment.setAvatar(userInfo.getAvatar());
        comment.setNickname(userInfo.getNickname());

        log.info(comment.toString());
        eduCommentService.save(comment);
        return R.ok();
    }
}
